** This dofile makes the results related to changes in Mexican exports to the United States from 2004 to 2007

/*
Table 2 (A and B): Changes in Mexican Textile/Apparel Incumbent Exports to the United States from 2004 to 2007 (Million USD)

Table 2 (C and D): Intensive Margin Changes in Mexican Textile/Apparel Incumbent Exports to the United States from 2004 to 2007 (Million USD)

Table A7: Decomposition of Extensive Margin
*/

** For Table 2
** Partner Margin Decomposition - Aggregate decomposition
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict", clear
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
drop if _merge==2
rename iq_mex_bind80 Binding
collapse (sum) value_exporter_importer, by(hs6 rfc newclid2 Binding year)
keep if year==2004 | year==2007
rename value value
reshape wide value, i(rfc newclid2 hs6) j(year)
drop if Binding==.
bys rfc hs6: egen firmexport2004=sum(value2004)
bys rfc hs6: egen firmexport2007=sum(value2007)
gen ntemp2004=(value2004!=.)
gen ntemp2007=(value2007!=.)
bys rfc hs6: egen firmnbuyer2004=sum(ntemp2004)
bys rfc hs6: egen firmnbuyer2007=sum(ntemp2007)
keep if firmexport2004!=0
assert firmnbuyer2004!=0
gen type=0
replace type=1 if value2004!=. & value2007!=. 
replace type=2 if value2004==. & value2007!=. 
replace type=3 if value2004!=. & value2007==. 
replace type=4 if value2004!=. & value2007==. & firmexport2007!=0 
replace value2004=value2004/1000000
replace value2007=value2007/1000000




save "${temp_data}total.dta", replace

use "${temp_data}total.dta", clear
collapse (sum) value2004 value2007, by(type Binding)
sort Binding type
rename value2007 valueF
rename value2004 valueI
reshape wide valueI valueF, i(Binding) j(type)
gen total=valueF4+valueF3+valueF2+valueF1-valueI4-valueI3-valueI2-valueI1
gen extensive=-valueI3
gen intensive=total-extensive
gen stay=valueF1-valueI1
gen add=valueF2-valueI2
gen drop=valueF4-valueI4
gen reallocation=abs(add)+abs(drop)-abs(add+drop)
keep Binding total extensive intensive stay add drop reallocation
order Binding total extensive intensive stay add drop reallocation
foreach var of varlist intensive stay add drop reallocation {
gen r_`var' =`var'*100 /intensive
}
replace r_reallocation=abs(r_reallocation)
save "${output}export_change/aggregate_decomposition.dta", replace


** Partner Margin Decomposition - hs6 product-level regression coefficients
use "${temp_data}total.dta", replace
collapse (sum) value2004 value2007, by(type hs6 Binding)
rename value2007 valueF
rename value2004 valueI
reshape wide valueI valueF, i(hs6) j(type)
local i = 1
while `i' <=4 {
replace valueF`i'=0 if valueF`i'==.
replace valueI`i'=0 if valueI`i'==.
local i = `i' + 1
}
gen total=valueF4+valueF3+valueF2+valueF1-valueI4-valueI3-valueI2-valueI1
gen extensive=-valueI3
gen intensive=total-extensive
gen p_stay=valueF1-valueI1
gen p_add=valueF2-valueI2
gen p_drop=valueF4-valueI4
gen reallocation=abs(p_add)+abs(p_drop)-abs(p_add+p_drop)
gen hs2=substr(hs6,1,2)
destring hs2, replace

eststo clear
eststo: xi:reg total Binding i.hs2, cluster(hs6)
eststo: xi:reg extensive Binding i.hs2, cluster(hs6)
eststo: xi:reg intensive Binding i.hs2, cluster(hs6)
eststo: xi:reg p_stay Binding i.hs2, cluster(hs6)
eststo: xi:reg p_add Binding i.hs2, cluster(hs6)
eststo: xi:reg p_drop Binding i.hs2, cluster(hs6)
eststo: xi:reg reallocation Binding i.hs2, cluster(hs6)

keep if e(sample)
gen hs6_sample=1
keep hs6_sample hs6 Binding
save "${temp_data}hs6_sample.dta", replace

esttab using "${output}export_change/partner_margin_coef.tex", replace nocons nonumbers indicate("Hs2-level Effects=_Ihs2*") starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3)))  stats(N, fmt(%9.0f) labels(Obs.)) mgroups("Total" "Extensive" "Intensive" "Stay" "Add" "Drop" "Reallocation", pattern(1 1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span)  posthead( &(1)&(2)&(3)&(4)&(5)&(6)&(7)\\  \hline) nomtitles varlabels((d) ) collabels(,none) substitute((d)) nomtitles



** For Table 3
** Main Partner Margin Decomposition - Aggregate decomposition
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict", clear
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
drop if _merge==2
rename iq_mex_bind80 Binding
collapse (sum) value_exporter_importer, by(hs6 rfc newclid2 Binding year)
keep if year==2004   |  year==2007
rename value value
reshape wide value, i(rfc newclid2 hs6) j(year)
drop if Binding==.
bys rfc hs6: egen firmexport2004=sum(value2004)
bys rfc hs6: egen firmexport2007=sum(value2007)
gen ntemp2004=(value2004!=.)
gen ntemp2007=(value2007!=.)
bys rfc hs6: egen firmnbuyer2004=sum(ntemp2004)
bys rfc hs6: egen firmnbuyer2007=sum(ntemp2007)
keep if firmexport2004!=0 & firmexport2007!=0
assert firmnbuyer2004!=0
gsort rfc hs6 -value2004
bys rfc hs6: gen order2004=_n
gen main2004=0
replace main2004=1 if order2004==1
gsort rfc hs6 -value2007
bys rfc hs6: gen order2007=_n
gen main2007=0
replace main2007=1 if order2007==1
replace value2004=value2004/1000000
replace value2007=value2007/1000000
save "${temp_data}total.dta", replace

use "${temp_data}total.dta", clear
collapse (sum) value2004 value2007, by(main2004 main2007 Binding)
sort Binding main2004 main2007
rename value2007 valueF
rename value2004 valueI
reshape wide valueI valueF, i(Binding main2007) j(main2004)
reshape wide value**, i(Binding) j(main2007)
gen d_mpartnerstay=valueF11-valueI11
gen d_mpartnerswitch=valueF01-valueI10
gen total=(valueF11+valueF10+valueF01+valueF00)-(valueI11+valueI10+valueI01+valueI00)
gen non_MP=total-d_mpartnerstay-d_mpartnerswitch
gen mp_stay=d_mpartnerstay
gen mp_add=valueF01
gen mp_drop=-valueI10
gen mp_excess=abs(mp_add)+abs(mp_drop)-abs(mp_add+mp_drop)
keep Binding total non_MP mp_stay mp_add mp_drop mp_excess 
order Binding total non_MP mp_stay mp_add mp_drop mp_excess
foreach var of varlist total non_MP mp_stay mp_add mp_drop mp_excess {
gen r_`var' =`var'*100 /total
}		
replace r_mp_add=abs(r_mp_add)
replace r_mp_excess=abs(r_mp_excess)
save "${output}export_change/aggregate_decomposition_main_partner.dta", replace


** Main Partner Margin Decomposition - HS six-digit product-level regression coefficients
use "${temp_data}total.dta", replace
collapse (sum) value2004 value2007, by(main2004 main2007 hs6 Binding)
rename value2007 valueF
rename value2004 valueI
reshape wide valueI valueF, i(hs6 main2007) j(main2004)
reshape wide value**, i(hs6) j(main2007)
merge 1:1 hs6 using "${temp_data}hs6_sample.dta"
local i = 0
while `i' <=1 {
local j = 0
while `j' <=1 {
replace valueF`i'`j'=0 if valueF`i'`j'==.
replace valueI`i'`j'=0 if valueI`i'`j'==.
local j= `j' + 1
}
local i = `i' + 1
}
gen mp_stay=valueF11-valueI11
gen d_mpartnerswitch=valueF01-valueI10
gen d_total=(valueF11+valueF10+valueF01+valueF00)-(valueI11+valueI10+valueI01+valueI00)
gen non_mp=d_total-mp_stay-d_mpartnerswitch
gen mp_add=valueF01
gen mp_drop=-valueI10
gen mp_excess=abs(mp_add)+abs(mp_drop)-abs(mp_add+mp_drop)
gen hs2=substr(hs6,1,2)
destring hs2, replace

eststo clear
eststo: xi:reg d_total Binding i.hs2, cluster(hs6)
eststo: xi:reg non_mp Binding i.hs2, cluster(hs6)
eststo: xi:reg mp_stay Binding i.hs2, cluster(hs6)
eststo: xi:reg mp_add Binding i.hs2, cluster(hs6)
eststo: xi:reg mp_drop Binding i.hs2, cluster(hs6)
eststo: xi:reg mp_excess Binding i.hs2, cluster(hs6)

esttab using "${output}export_change/main_partner_margin_coef.tex", replace nocons nonumbers indicate("Hs2-level Effects=_Ihs2*")  starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3)) )  stats(N, fmt(%9.0f) labels(Obs.)) mgroups("Total"  "Non MP" "MP Stay" "MP Add" "MP Drop" "MP Excess", pattern(1 1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span)  posthead( &(1)&(2)&(3)&(4)&(5)&(6)\\  \hline) nomtitles varlabels((d)) collabels(,none) substitute((d)) nomtitles


** For Table A7
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict", clear
merge m:1 hs6 using "${output_data}quota_mex_weight_hs6.dta"
drop if _merge==2
rename iq_mex_bind80 Binding
collapse (sum) value_exporter_importer, by(hs6 rfc newclid2 Binding year)
keep if year==2004   |  year==2007
rename value value
collapse (sum) value,by(year hs6 rfc Binding)
reshape wide value, i(rfc hs6) j(year)
drop if Binding==.
bys rfc: egen firmexport2004=sum(value2004)
bys rfc: egen firmexport2007=sum(value2007)
gen ntemp2004=(value2004!=.)
gen ntemp2007=(value2007!=.)
bys rfc: egen firmnbuyer2004=sum(ntemp2004)
bys rfc: egen firmnbuyer2007=sum(ntemp2007)
keep if firmexport2004!=0
assert firmnbuyer2004!=0
gen type=0
replace type=1 if value2004!=. & value2007==. & firmexport2007==0
replace type=2 if value2004!=. & value2007==. & firmexport2007!=0
replace value2004=value2004/1000000
replace value2007=value2007/1000000
collapse (sum) value2004 value2007, by(type Binding)
sort Binding type
rename value2007 valueF
rename value2004 valueI
reshape wide valueI valueF, i(Binding) j(type)
gen total_extensive = -(valueI1+valueI2)
gen leaving = -valueI1
gen dropping = -valueI2
keep Binding total_extensive leaving dropping
foreach var of varlist total_extensive leaving dropping {
gen r_`var' =`var'*100 /total_extensive
        }
save "${output}export_change/decomposition_extensive.dta", replace

